using Microsoft.AspNetCore.Mvc;
using newApi.Models;
using newApi.Services;

namespace newApi.Controllers;

[ApiController]
[Route("/[controller]")]
public class ChanelsController : ControllerBase
{
    private readonly ChanelsService _chanelsService;
    

    public ChanelsController(ChanelsService chanelsServices)
    {
        _chanelsService=chanelsServices;
    }

    // [HttpGet]
    // public async Task<List<string>> Get(){
    //      var chanels = await _chanelsService.GetAsync();
    //      List<string> newdata = new List<string>();
    //      for(int i=0;i<chanels.Count();i++){
    //          newdata.Add(chanels[i].title);
    //      }
    //      return newdata;
    // }
    [HttpGet]
    public async Task<IActionResult> Get(){
        List<string> result =new List<string>();
        var chanels =await _chanelsService.GetAsync();
        foreach(var c in chanels){
            result.Add(c.Title);
        }
        return Ok(result);
    }
   

    // [HttpGet]
    // public async Task CreateChanels(Chanels newChanel) =>
    // await _chanelsService.GetAsync();
    [HttpGet("{id:length(24)}")]
    public async Task<ActionResult<List<Chanel>>> Get(string id)
    {
        var chanels = await _chanelsService.GetAsync(id);

        if (chanels is null)
        {
            return NotFound();
        }

        return chanels;
    }

    [HttpPost]
    public async Task<IActionResult> Post(Chanel newChanels)
    {
        await _chanelsService.CreateAsyn(newChanels);
        return CreatedAtAction(nameof(Get), new { id = newChanels.Id }, newChanels);
    }

    [HttpPut("{id:length(24)}")]
    public async Task<IActionResult> Update(string id, Chanel updateChanels)
    {
        var chanels = await _chanelsService.GetAsync(id);

        if (chanels is null)
        {
            return NotFound();
        }

        updateChanels.Id = chanels[0].Id;

        await _chanelsService.UpdateAsync(id, updateChanels);

        return NoContent();
    }
    
    [HttpDelete("{id:length(24)}")]
    public async Task<IActionResult> Delete(string id)
    {
        var chanels = await _chanelsService.GetAsync(id);

        if (chanels is null)
        {
            return NotFound();
        }

        await _chanelsService.RemoveAsync(id);

        return NoContent();
    }
    
}